#ifndef __OFP_CONFIG__
#define __OFP_CONFIG__

#include <stdint.h>
#include <glib.h>
#include "common.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
    参数配置文件ofp_drv.conf,是驱动的配置文件
    里面包含DeviceParam 设备参数组，UpgradeParam升级相关参数组
    例如：VID=0x33a7, VID是键值对的key, 0x33a7是value
*/
#define OFP_DRIVER_CONFIG_FILE "ofp_drv.conf"

#define OFP_GROUP_CONF_DEVICE_PARAM "DeviceParam"
#define OFP_KEY_CONF_USB_VID "usb_vid"
#define OFP_KEY_CONF_MCU_NAME               "mcu_name"
#define OFP_KEY_CONF_FIRMWARE_NAME          "firmware_name"
#define OFP_KEY_CONF_FIRMWARE_VERSION       "firmware_version"
#define OFP_KEY_CONF_BUS_TYPE               "bus_type"

#define OFP_GROUP_CONF_UPGRADE_PARAM "UpgradeParam"
#define OFP_KEY_CONF_UPGRADE_MODE           "upgrade_mode"
#define OFP_KEY_CONF_NEED_UPGRADE_FLAG      "need_upgrade_flag"
#define OFP_KEY_CONF_USB_BOOT_VID "usb_boot_vid"


#define OFP_KEY_VALUE_FORCE_UPGRADE_MODE "FORCE_UPGRADE"
#define OFP_KEY_VALUE_DONT_UPGRADE_MODE "DONT_UPGRADE"
#define OFP_KEY_VALUE_AUTO_UPGRADE_MODE "AUTO_UPGRADE"
#define OFP_KEY_VALUE_SMART_UPGRADE_MODE "SMART_UPGRADE"


#define OFP_GROUP_CONF_DRIVER_PARAM  "DriverParam"
#define OFP_KEY_CONF_DRIVER_NAME     "driver_name"
#define OFP_KEY_CONF_ENROLL_TIMES  "enroll_times"
#define OFP_KEY_CONF_ENROLL_MIN_ID   "enroll_min_id"
#define OFP_KEY_CONF_ENROLL_MAX_ID   "enroll_max_id"
#define OFP_KEY_CONF_SYSTEM_NAME     "system_name"
#define OFP_KEY_CONF_ENROLL_TIMEOUT  "enroll_timeout"
#define OFP_KEY_CONF_LOG_LEVEL       "log_level"

#define OFP_KEY_VALUE_WINDOWS_SYSTEM "WINDOWS"
#define OFP_KEY_VALUE_KYLIN_SYSTEM "KYLIN"
#define OFP_KEY_VALUE_UOS_SYSTEM "UOS"
#define OFP_KEY_VALUE_NFS_SYSTEM "NFS"



#define OFP_NEED_UPGRADE_FLAG  1
#define OFP_NOT_NEED_UPGRADE_FLAG  0

#define OFP_DRIVER_NAME_LEN_MAX 50
#define OFP_MCU_NAME_LEN_MAX 50
#define OFP_FIRMWARE_NAME_LEN_MAX 50

// 定义升级模式
typedef enum {
    FORCE_UPGRADE = 0,//不判断版本，强制升级
    DONT_UPGRADE = 1,//不执行升级
    AUTO_UPGRADE = 2,//判断版本不同则升级
    SMART_UPGRADE = 3,//判断新版本高于实际版本则升级
    UPGRADE_MODE_MAX = 4,
} ofp_upgrade_mode;

//指纹类型
typedef enum {
    UART_TYPE = 1u, /* 串口指纹 */
    USB_TYPE = 2u,  /* USB指纹 */
    BUS_TYPE_MAX = 3u,  /* USB指纹 */
} ofp_bus_type;

typedef enum {
    DEVICE_CCM4201S = 0u,
    DEVICE_MAX    = 1u,
} ofp_mcu_type;

typedef enum {
	FP_WINDOWS_OS_TYPE = 0,	//windows
	FP_KYLIN_OS_TYPE = 1,	//麒麟
	FP_UOS_OS_TYPE = 2,		//统信操作系统
	FP_NFS_OS_TYPE = 3,		//方德操作系统
	FP_ALL_OS_TYPE = 0xff,	//搜索全部
	FP_NONE_OS_TYPE = 0xffff,
} ofp_os_type;


typedef int32_t (*ofp_upgrade_fun)(void *handle, void *upgrade_firmware);
typedef struct {
    uint8_t *mcu_name;
    ofp_mcu_type mcu_type;
    ofp_bus_type bus_type;  //指纹总线类型，USB还是UART
    ofp_upgrade_fun mcu_upgrade_fun;//升级函数，其他函数后面继续添加,于MCU 强相关
} ofp_mcu_drv_info;

typedef struct {
    uint8_t *mcu_name;
    uint8_t *bus_type;
    uint8_t *usb_vid;
} ofp_drv_conf_key_device_param_group;

typedef struct {
    uint32_t need_upgrade_flag;
    uint8_t *upgrade_mode;
    uint8_t *boot_usb_vid;
    uint8_t *firmware_name;
    uint8_t *firmware_version;
} ofp_drv_conf_key_upgrade_param_group;

typedef struct {
    uint8_t *driver_name;
    uint32_t enroll_times;
    uint32_t enroll_min_id;
    uint32_t enroll_max_id;
    uint8_t *system_name;
    uint32_t enroll_timeout;
    uint32_t log_level;
} ofp_drv_conf_key_driver_param_group;

//读取文件对于键值对缓存的键值
typedef struct {
    ofp_drv_conf_key_device_param_group device_param_group;
    ofp_drv_conf_key_upgrade_param_group upgrade_param_group;
    ofp_drv_conf_key_driver_param_group driver_param_group;
} ofp_drv_conf_key_value_param;

typedef struct {
    ofp_upgrade_mode mode;
    uint8_t *mode_sring;
    uint8_t *description;
} ofp_upgrade_mode_info;

typedef struct {
    uint8_t mcu_name[OFP_MCU_NAME_LEN_MAX];
    uint16_t usb_vid;//指纹vid

    ofp_mcu_type mcu_type;
    ofp_bus_type bus_type;
} ofp_drv_device_param_conf;

typedef struct {
    uint32_t need_upgrade_flag;
    ofp_upgrade_mode upgrade_mode;
    uint16_t boot_usb_vid;//指纹进入boot状态vid
    uint8_t firmware_name[OFP_FIRMWARE_NAME_LEN_MAX];
    uint8_t firmware_version[OFP_FIRMWARE_VERSION_LEN_MAX];

} ofp_drv_upgrade_param_conf;

typedef struct {
    uint8_t driver_name[OFP_DRIVER_NAME_LEN_MAX];
    uint32_t enroll_times;
    uint32_t enroll_min_id;
    uint32_t enroll_max_id;
    uint32_t enroll_timeout;
    uint32_t log_level;
    ofp_os_type system_id;
} ofp_drv_driver_param_conf;

typedef struct {
    GKeyFile *drv_cfg_file;
    ofp_drv_device_param_conf device_param_conf;
    ofp_drv_upgrade_param_conf upgrade_param_conf;
    ofp_drv_driver_param_conf driver_param_conf;
} ofp_drv_conf;


extern const ofp_mcu_drv_info g_mcu_drv_info[DEVICE_MAX];
int32_t ofp_dev_open_drv_config(ofp_drv_conf *drv_cfg, uint8_t *file_name);
int32_t ofp_dev_get_drv_config(ofp_drv_conf *drv_cfg);
int32_t ofp_dev_set_drv_upgrade_flag(ofp_drv_conf *drv_cfg, int32_t value);
void ofp_dev_close_drv_config(ofp_drv_conf *drv_cfg);



#ifdef __cplusplus
}
#endif

#endif


